<template>
	<div>
		<view class="wrap">
			<block v-if="list.length > 0">
				<view class="wrap-info">
					<view @click="onItem(list[0])">
						<view class="info-title">
							{{ list[0].title }}
						</view>

						<image
							class="info-file"
							mode="widthFix"
							:src="getImageURL(list[0].cover)"
							lazy-load
							@error="onImageError(0)"
						/>

						<view class="info-time-bg">
							<view class="time" v-if="list.length === 1"> 比赛时间: {{ list[0].competitionTime.join("~") }} </view>
							<view class="time" v-else></view>
							
							<view class="btn" v-if="list[0].canApply == 1">开始报名</view>
							<view class="btn bbttnn" v-else>已报名</view>
						</view>
					</view>
				</view>
			</block>

			<view class="info-assistant">
				<view class="assistant-title">
					<image class="img" src="/static/images/assistant-line.png" />
					<view class="title"> 赛事助手 </view>
					<image class="img" src="/static/images/assistant-line.png" />

					<view class="title-eng"> EVENT ASSISTANT </view>
				</view>

				<view class="assistant-list">
					<block v-for="(item, index) in assistant_list" :key="index">
						<view class="item" @click="onAssistant(item)">
							<image
								class="img"
								:src="getImageURL(item.file)"
								lazy-load
								@error="onAssistantImageError(index)"
							/>

							<view class="name">
								{{ item.title }}
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
	</div>
</template>

<script>
	import { getCompetitionList } from "@/utils/api.js";
	export default {
		data() {
			return {
				assistant_list: [],
				list: [],
			};
		},
		onShow() {
			const loginStatus = this.$filters.isLogin();
			if (loginStatus == true) {
				this.getData();
			}
		},
		onLoad() {
			this.$filters.getConfig((data) => {
				this.assistant_list = [
					{
						name: "赛事章程",
						title: data.competitionCharterName,
						file: data.competitionCharterLogo,
						url: data.competitionCharter,
						isLink: 1,
					},
					{
						name: "赛事须知",
						title: data.competitionNoticeName,
						file: data.competitionNoticeLogo,
						url: data.competitionNotice,
						isLink: 1,
					},
					{
						name: "参赛号查询",
						title: data.competitionNoQueryName,
						file: data.competitionNoQueryLogo,
						url: data.competitionNoQuery,
						isLink: 1,
					},
					{
						name: "成绩查询",
						title: data.competitionScoreQueryName,
						file: data.competitionScoreQueryLogo,
						url: data.competitionScoreQuery,
						isLink: 1,
					},
					{
						name: "奖项证书查询",
						title: data.competitionRewardQueryName,
						file: data.competitionRewardQueryLogo,
						url: data.competitionRewardQuery,
						isLink: 1,
					},
					{
						name: "赛事指引",
						title: data.competitionGuideName,
						file: data.competitionGuideLogo,
						// url: "/pages/eventGuide/index",
						url: data.competitionGuideLink,
						isLink: 1,
					},
				];
			});
		},
		methods: {
			/**
			 * 图片加载失败
			 */
			onImageError(index) {
				this.list[index].cover = "/static/common/default_icon.png";
			},
			onAssistantImageError(index) {
				this.assistant_list[index].file = "/static/common/default_icon.png";
			},
			/**
			 * 列表点击
			 */
			onItem(item) {
				if (this.list.length > 1) {
					uni.navigateTo({
						url: "/pages/registration/list"
					})
				} else {
					uni.navigateTo({
						url: `/pages/registration/eventDetails?id=${item.competitionId}`,
					});
				}
			},
			/**
			 * 赛事助手点击
			 */
			onAssistant(item) {
				if (this.$filters.isGoToExternalLink(item.isLink, item.url)) {
					uni.navigateTo({
						url: item.url,
					});
				}
			},
		
			/**
			 * 网络请求
			 */
			getData() {
				getCompetitionList(1).then((res) => {
					this.list = res.data.data;
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #f6f6f6;
	}
	.wrap {
		--limit-num: 1;
		width: 100%;
		overflow: hidden;
		background-color: white;

		.wrap-info {
			width: 100%;
			overflow: hidden;

			.info-title {
				width: 100%;
				height: 86rpx;
				line-height: 86rpx;
				text-align: center;
				color: #333333;
				font-size: 30rpx;
				font-weight: bold;
				padding: 0 20rpx;
				box-sizing: border-box;
			}

			.info-file {
				width: 750rpx;
			}
			.info-time-bg {
				width: 100%;
				height: 140rpx;
				background-color: #ea5520;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 30rpx;
				box-sizing: border-box;

				.time {
					flex: 1;
					margin-right: 10rpx;
					color: white;
					font-size: 28rpx;
				}
				.btn {
					width: 194rpx;
					height: 74rpx;
					line-height: 74rpx;
					text-align: center;
					background-color: white;
					border-radius: 40rpx;
					overflow: hidden;
					font-size: 28rpx;
					color: #ea5520;
				}
			}
		}

		.info-assistant {
			margin-top: 60rpx;
			width: 100%;
			overflow: hidden;

			.assistant-title {
				width: 100%;
				overflow: hidden;
				padding-bottom: 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;

				.img {
					width: 158rpx;
					height: 2rpx;
				}
				.title {
					margin-left: 22rpx;
					margin-right: 22rpx;
					color: #333333;
					font-size: 42rpx;
					font-weight: bold;
				}
				.title-eng {
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
					color: #666666;
					font-size: 18rpx;
				}
			}

			.assistant-list {
				margin-top: 30rpx;
				width: 100%;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				padding: 0 30rpx;
				box-sizing: border-box;

				.item {
					width: calc(100% / 3);
					padding: 30rpx 0;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 30rpx;

					.img {
						width: 62rpx;
						height: 62rpx;
					}
					.name {
						margin-top: 24rpx;
						color: #333333;
						font-size: 28rpx;
					}
				}
			}
		}
	}
	view {
		line-height: 1;
	}
</style>
